package cn.mrlong.base.utils

import android.view.View
import cn.mrlong.base.R

/**
 * 作者：BINGO
 * 时间：2021/03/29 10:21
 * 防止重复点击
 * todo 如果view tag 被使用，请勿使用改方法
 */
class OnRepeatClick {
    companion object {
        //默认重复点击间隔时间
        private val INTERNAL_TIME: Long = 1000

        /**
         * 判断是否间隔时间点击
         * @param view         View 需要判断的view
         * @param internalTime Long 需要间隔的时间 = 默认1000
         * @return Boolean
         */
        fun isInValidClick(view: View, internalTime: Long = INTERNAL_TIME): Boolean {
            val curTimeStamp = System.currentTimeMillis()
            var lastClickTimeStamp: Long = 0
            var target: Any? = view.getTag(R.id.last_click_time)
            if (target == null) {
                view.setTag(R.id.last_click_time, curTimeStamp)
                return false
            }
            lastClickTimeStamp = target as Long
            val temp = (curTimeStamp - lastClickTimeStamp)
            val isInvalid: Boolean = temp < internalTime
            if (!isInvalid) {
                view.setTag(R.id.last_click_time, curTimeStamp)
            }
            return isInvalid
        }
    }
}